﻿package {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import asunit.framework.TestCase;					

	/**
	 * @author colin
	 */
	public class EventDispatcherTest extends TestCase {
		private var _instance:ED;
		private var strTest:String;

		public function EventDispatcherTest(testMethod : String = null) {
			super(testMethod);
		}
		protected override function setUp():void {
	 		_instance = new ED();
	 		strTest="";
		}

		protected override function tearDown():void {
			strTest = null;
	 		_instance = null;
	 	}

	 	public function testInstantiated():void {
	 		assertTrue("Example instantiated", _instance is EventDispatcher);
	 	}
	 	
	 	public function testAddDoubleListener():void{
	 		strTest = "";
			_instance.addEventListener(Event.INIT, addListenerA);
			_instance.addEventListener(Event.INIT, addListenerA);
	 		_instance.addEventListener(Event.INIT, addListenerB);
	 		_instance.test(false, true);

	 		assertEquals("Expected:AB Received:"+strTest, strTest, 'AB');
	 	}

	 	public function testAddRemoveSelfListener():void{
	 		_instance.addEventListener(Event.INIT, addListenerB);
			_instance.addEventListener(Event.INIT, addListenerC);
			_instance.addEventListener(Event.INIT, addListenerA);
	 		_instance.test(false,true);
			_instance.test(false,true);

	 		assertEquals("Expected:BCABA Received:"+strTest, strTest, 'BCABA');
	 	}

	 	public function testAddRemovePreviousListener():void{
	 		_instance.addEventListener(Event.INIT, addListenerB);
			_instance.addEventListener(Event.INIT, addListenerA);
			_instance.addEventListener(Event.INIT, addListenerD);
	 		_instance.test(false,true);
			_instance.test(false,true);

	 		assertEquals("Expected:BADBD Received:"+strTest, strTest, 'BADBD');
	 	}

	 	public function testAddRemoveNextListener():void{
	 		_instance.addEventListener(Event.INIT, addListenerB);
			_instance.addEventListener(Event.INIT, addListenerD);
			_instance.addEventListener(Event.INIT, addListenerA);
	 		_instance.test(false,true);
			_instance.test(false,true);

	 		assertEquals("Expected:BDABD Received:"+strTest, strTest, 'BDABD');
	 	}
	 	
	 	private function addListenerA(...e):void{
	 		strTest += 'A';
	 	}

	 	private function addListenerB(...e):void{
	 		strTest += 'B';
	 	}

	 	private function addListenerC(...e):void{
	 		_instance.removeEventListener(Event.INIT, addListenerC);
	 		strTest += 'C';
	 	}

	 	private function addListenerD(...e):void{
	 		_instance.removeEventListener(Event.INIT, addListenerA);
	 		strTest += 'D';
	 	}
	}
}

import flash.events.Event;
import flash.events.EventDispatcher;

class ED extends EventDispatcher{
	public function ED(){
	
	}
	
	public function test(bubbles:Boolean,cancelable:Boolean):void{
		dispatchEvent(new Event(Event.INIT,bubbles,cancelable));
	}
}
